// pages/signIn/signIn.js
//获取应用实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //img_url: config.imgUrl, //图片地址

    //签到模块
    signNum: 0, //签到数
    signTotalNum: 0, //签到数
    signState: false, //签到状态
    min: 1, //默认值日期第一天1
    max: 7, //默认值日期最后一天7
    be: 0 //默认倍数


  },

  //签到
  bindSignIn(e) {
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    });

    var sessionId = app.globalData.sessionId;
    var param = {
      sessionKey: sessionId
    };
    wx.request({
      url: app.url.doSignIn,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: param,
      success: function(res) {
        wx.hideLoading();
        const data = res.data;
        if (data.result == "true") {
          var num = e.currentTarget.dataset.num;
          num++
          wx.showToast({
            icon: 'success',
            title: '签到成功',
          });
          that.setData({
            signNum: num,
            signState: true
          });
          var be = e.currentTarget.dataset.be;
          if (num % 7 == 0) {
            be += 1;
            that.setData({
              be: be
            });
          }
          if (num == 7 * be + 1) {
            that.setData({
              min: 7 * be + 1,
              max: 7 * be + 7
            });
          }
        } else {
          wx.showToast({
            icon: 'error',
            title: '签到失败',
          });
        }

      },
      fail: res => {
        wx.hideLoading();
        wx.showModal({
          content: '签到失败',
          showCancel: false,
          success: function(res) {}
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    });

    var sessionId = app.globalData.sessionId;
    var param = {
      sessionKey: sessionId
    };
    wx.request({
      url: app.url.getTodaySignInData,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: param,
      success: function(res) {
        wx.hideLoading();
        const data = res.data;
        var num = data.seriesCount;
        that.setData({
          signNum: num,
          signTotalNum: data.totalCount,
          signState: data.todaySignState
        });
        var be = parseInt(num / 7);
        if (num > 0 && num % 7 == 0) {
          be -= 1;
        }
        if (num % 7 == 0) {
          that.setData({
            min: 7 * be + 1,
            max: 7 * be + 7
          });
        } else {
          that.setData({
            min: 7 * be + 1,
            max: 7 * (be + 1)
          });
        }
        if (num > 0 && num % 7 == 0) {
          be += 1;
        }
        that.setData({
          be: be
        });
      },
      fail: res => {
        wx.hideLoading();
        wx.showModal({
          content: '获取签到天数错误',
          showCancel: false,
          success: function(res) {}
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})